﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace AutumnEngine
{
    public class MouseNode : SpriteNode
    {
        public MouseNode() :
            base("Mouse")
        {

        }
        public override void Update()
        {
            if (Engine.Modules.Contains("Engine.MouseInput"))
                this.Position = Engine.Modules.MouseInput.TransformedPosition;
            base.Update();
        }
    }
    public class MainMap : Scene
    {
        public MainMap()
        {
           //Load texture for mouse.          
            Engine.Modules.TextureManager.AddTexture("Mouse", "Particle");
            Engine.Modules.SoundManager.AddSound("Bop", "Bop");
            //Make mouse node.
            this.AddNode("Mouse", new MouseNode());

            Engine.Modules.FontManager.AddFont("MainFont", "Fonts/Main");
            LabelNode l = new LabelNode("MainFont");
            l.Text = "Press F6 to load.\nPress F5 to save.\nPress P to set wind to mouse position.\nWASD to move camera.\nQ/E to rotate camera.\nRight Mouse to cause explosion at mouse.";
            l.Color = Color.DarkGray;
#if ZUNE
            l.Text = "3 Taps To Load.\n2 Taps For Sound";
            l.Scale = 0.45f;
#endif
            l.Position = new Vector2(-340, -100);
            this.AddNode("InstructionLabel", l);



            bool testScene = false;
            //Uncomment for a test scene.
            //testScene = true;
            if(testScene)
            {
            LabelNode label = new LabelNode("MainFont");

#if ZUNE
            label.Text = "Autumn Engine v0.6 Stable Branch";
            label.Rotation = MathHelper.ToRadians(90);
            label.Scale = 0.45f;
            label.Origin = new Vector2(0, Engine.Modules.FontManager.MeasureString(label.Text, "MainFont").Y / 2);
#else
            label.Text = "Autumn\nEngine\nv0.6-Stable\n";            
#endif

            label.Position = new Vector2(15, 200);
            label.Color = Color.White;
            label.CameraTranslation = false;
            this.AddNode("Label", label);

            Engine.Modules.TextureManager.AddTexture("ManTexture", "TestingTexture");
            Engine.Modules.TextureManager.AddTexture("Platform", "Platform");
            //Create the physic node.
            PhysicsSpriteNode phys = new PhysicsSpriteNode("ManTexture",BodyType.Automatic, 64f, 128f, 10f);
            phys.Color = Color.Green;
            this.AddNode("Physic", phys);

            //We'll make a platform for the physic node.
            PhysicsSpriteNode platform = new PhysicsSpriteNode("Platform", BodyType.Rectangular, 1280, 40f, 10f);
            platform.Position = new Vector2((1280 - 600), 480 - 20);
            platform.Static = true;
            this.AddNode("Platform", platform);

            }           
          
        }

        public override void Update()
        {
            if (Engine.Modules.Contains("Engine.MouseInput"))
            {
                if (this.Nodes.ContainsKey("Physic"))
                    if (Engine.Modules.MouseInput.IsLeftButtonDown)
                    {
                        this.Nodes["Physic"].Position = Engine.Modules.MouseInput.TransformedPosition;                     
                            Engine.Modules.SoundManager.PlaySound("Bop", false);                                                
                    }
                if (Engine.Modules.MouseInput.IsRightButtonTriggered)
                    Engine.Modules.PhysicsManager.ApplyExplosion(Engine.Modules.MouseInput.TransformedPosition, new Vector2(400, 400));
               this.CameraZoom += Engine.Modules.MouseInput.MouseWheelVelocity / 1000;
            }
          
            if (Engine.Modules.Contains("Engine.KeyboardInput"))
            {
                if (Engine.Modules.KeyboardInput.IsKeyTriggered(Microsoft.Xna.Framework.Input.Keys.F5))                
                    this.Save("Content/Test.asf");
                
                if (Engine.Modules.KeyboardInput.IsKeyTriggered(Microsoft.Xna.Framework.Input.Keys.F6))
                {
                    this.Load("Content/Test.asf");
                    //Make mouse node.
                    this.AddNode("Mouse", new MouseNode());
                }

                if (Engine.Modules.KeyboardInput.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.W))                
                    this.CameraPosition += new Vector2(0, -5);
                
                if (Engine.Modules.KeyboardInput.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.S))                
                    this.CameraPosition += new Vector2(0, 5);
                

                if (Engine.Modules.KeyboardInput.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.A))                
                    this.CameraPosition += new Vector2(-5,0);
                
                if (Engine.Modules.KeyboardInput.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.D))                
                    this.CameraPosition += new Vector2(5, 0);
                

                if (Engine.Modules.KeyboardInput.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.E))                
                    this.CameraRotation -= 0.01f;
                

                if (Engine.Modules.KeyboardInput.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Q))                
                    this.CameraRotation += 0.01f;
                

                if (Engine.Modules.KeyboardInput.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.P))
                    Engine.Modules.PhysicsManager.SetWind(Engine.Modules.MouseInput.TransformedPosition, 100, 20f);

                if (this.Nodes.ContainsKey("Physic"))
                {
                    if (Engine.Modules.KeyboardInput.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Right))
                        ((PhysicsSpriteNode)this.Nodes["Physic"]).ApplyForce(new Vector2(50, 0));
                    if (Engine.Modules.KeyboardInput.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left))
                        ((PhysicsSpriteNode)this.Nodes["Physic"]).ApplyForce(new Vector2(-50, 0));

                    if (Engine.Modules.KeyboardInput.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Up))
                        ((PhysicsSpriteNode)this.Nodes["Physic"]).ApplyForce(new Vector2(0, -500));
                    if (Engine.Modules.KeyboardInput.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Down))
                        ((PhysicsSpriteNode)this.Nodes["Physic"]).ApplyForce(new Vector2(0, 500));

                }
            }

            if (Engine.Modules.Contains("Engine.TouchscreenInput"))
            {
                if (Engine.Modules.TouchscreenInput.DoesTouchExist(2))
                {
                    this.Load("Content/Test.asf");
                    return;
                }
                if (Engine.Modules.TouchscreenInput.DoesTouchExist(1))
                {
                    Engine.Modules.SoundManager.PlaySound("Bop", false);
                    return;
                }
                if (Engine.Modules.TouchscreenInput.DoesTouchExist(0))
                {
                    TouchLocation cur = Engine.Modules.TouchscreenInput.CurrentTouches[0];
                    TouchLocation old;
                    if (cur.TryGetPreviousLocation(out old))
                    {
                        Vector2 dif = cur.Position - old.Position;
                        this.CameraPosition += -dif;
                    }
                }
            }
            base.Update();
        }
    }
}
